home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / FindDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  13.3 KB  |  425 lines  |  [TEXT/KAHL]

  1. /* FindDialog.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "FindDialog.h"
  31. #include "Screen.h"
  32. #include "EventLoop.h"
  33. #include "SimpleButton.h"
  34. #include "TextEdit.h"
  35. #include "Memory.h"
  36. #include "Alert.h"
  37. #include "Menus.h"
  38.  
  39.  
  40. #define WIDTH (490)
  41. #define HEIGHT (114)
  42.  
  43. #define SEARCHPROMPTX (13)
  44. #define SEARCHPROMPTY (11)
  45.  
  46. #define SEARCHEDITX (114)
  47. #define SEARCHEDITY (8)
  48. #define SEARCHEDITWIDTH (364)
  49. #define SEARCHEDITHEIGHT (22)
  50.  
  51. #define REPLACEPROMPTX (13)
  52. #define REPLACEPROMPTY (37)
  53.  
  54. #define REPLACEEDITX (114)
  55. #define REPLACEEDITY (34)
  56. #define REPLACEEDITWIDTH (364)
  57. #define REPLACEEDITHEIGHT (22)
  58.  
  59. #define FROMSTARTX (6)
  60. #define FROMSTARTY (74)
  61. #define FROMSTARTWIDTH (111)
  62. #define FROMSTARTHEIGHT (21)
  63.  
  64. #define FINDAGAINX (128)
  65. #define FINDAGAINY (74)
  66. #define FINDAGAINWIDTH (111)
  67. #define FINDAGAINHEIGHT (21)
  68.  
  69. #define DONTFINDX (249)
  70. #define DONTFINDY (74)
  71. #define DONTFINDWIDTH (111)
  72. #define DONTFINDHEIGHT (21)
  73.  
  74. #define CANCELX (369)
  75. #define CANCELY (74)
  76. #define CANCELWIDTH (111)
  77. #define CANCELHEIGHT (21)
  78.  
  79.  
  80. typedef struct
  81.     {
  82.         WinType*                            ScreenID;
  83.         TextEditRec*                    StringToFind;
  84.         TextEditRec*                    StringToReplace;
  85.         TextEditRec*                    ActiveTextEdit;
  86.         SimpleButtonRec*            FindFromStartButton;
  87.         SimpleButtonRec*            FindAgainButton;
  88.         SimpleButtonRec*            DontFindButton;
  89.         SimpleButtonRec*            CancelButton;
  90.     } FindDialogRec;
  91.  
  92.  
  93. static void        LocalUpdate(FindDialogRec* Window)
  94.     {
  95.         CheckPtrExistence(Window);
  96.         SetClipRect(Window->ScreenID,0,0,WIDTH,HEIGHT);
  97.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Search for:",11,
  98.             SEARCHPROMPTX,SEARCHPROMPTY,ePlain);
  99.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Replace with:",13,
  100.             REPLACEPROMPTX,REPLACEPROMPTY,ePlain);
  101.         RedrawSimpleButton(Window->FindFromStartButton);
  102.         RedrawSimpleButton(Window->FindAgainButton);
  103.         RedrawSimpleButton(Window->DontFindButton);
  104.         RedrawSimpleButton(Window->CancelButton);
  105.         TextEditFullRedraw(Window->StringToFind);
  106.         TextEditFullRedraw(Window->StringToReplace);
  107.     }
  108.  
  109.  
  110. FindOpType        DoFindDialog(char** SearchKey, char** Replace,
  111.                                 MenuItemType* MCut, MenuItemType* MPaste, MenuItemType* MCopy,
  112.                                 MenuItemType* MUndo, MenuItemType* MSelectAll, MenuItemType* MClear)
  113.     {
  114.         FindOpType                    ReturnCode;
  115.         FindDialogRec*            Window;
  116.  
  117.         /* make sure the things exist */
  118.         CheckPtrExistence(*SearchKey);
  119.         CheckPtrExistence(*Replace);
  120.  
  121.         Window = (FindDialogRec*)AllocPtrCanFail(sizeof(FindDialogRec),"FindDialogRec");
  122.         if (Window == NIL)
  123.             {
  124.              FailurePoint1:
  125.                 AlertHalt("There is not enough memory to display the dialog box.",NIL);
  126.                 return eFindCancel;
  127.             }
  128.  
  129.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  130.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WIDTH),
  131.             DialogTopEdge(HEIGHT),WIDTH,HEIGHT,(void (*)(void*))&LocalUpdate,Window);
  132.         if (Window->ScreenID == 0)
  133.             {
  134.              FailurePoint2:
  135.                 ReleasePtr((char*)Window);
  136.                 goto FailurePoint1;
  137.             }
  138.         SetWindowName(Window->ScreenID,"Find");
  139.  
  140.         Window->StringToFind = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  141.             9,SEARCHEDITX,SEARCHEDITY,SEARCHEDITWIDTH,SEARCHEDITHEIGHT);
  142.         if (Window->StringToFind == NIL)
  143.             {
  144.              FailurePoint3:
  145.                 KillWindow(Window->ScreenID);
  146.                 goto FailurePoint2;
  147.             }
  148.         TextEditNewRawData(Window->StringToFind,*SearchKey,""/*shouldn't be a linefeed*/);
  149.         TextEditDoMenuSelectAll(Window->StringToFind);
  150.         Window->ActiveTextEdit = Window->StringToFind;
  151.         EnableTextEditSelection(Window->ActiveTextEdit);
  152.  
  153.         Window->StringToReplace = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  154.             GetScreenFont(),9,REPLACEEDITX,REPLACEEDITY,REPLACEEDITWIDTH,REPLACEEDITHEIGHT);
  155.         if (Window->StringToReplace == NIL)
  156.             {
  157.              FailurePoint4:
  158.                 DisposeTextEdit(Window->StringToFind);
  159.                 goto FailurePoint3;
  160.             }
  161.         TextEditNewRawData(Window->StringToReplace,*Replace,""/*shouldn't be a linefeed*/);
  162.         TextEditDoMenuSelectAll(Window->StringToReplace);
  163.  
  164.         Window->FindFromStartButton = NewSimpleButton(Window->ScreenID,"Find From Start",
  165.             FROMSTARTX,FROMSTARTY,FROMSTARTWIDTH,FROMSTARTHEIGHT);
  166.         if (Window->FindFromStartButton == NIL)
  167.             {
  168.              FailurePoint5:
  169.                 DisposeTextEdit(Window->StringToReplace);
  170.                 goto FailurePoint4;
  171.             }
  172.  
  173.         Window->FindAgainButton = NewSimpleButton(Window->ScreenID,"Find Again",
  174.             FINDAGAINX,FINDAGAINY,FINDAGAINWIDTH,FINDAGAINHEIGHT);
  175.         if (Window->FindAgainButton == NIL)
  176.             {
  177.              FailurePoint6:
  178.                 DisposeSimpleButton(Window->FindFromStartButton);
  179.                 goto FailurePoint5;
  180.             }
  181.         SetDefaultButtonState(Window->FindAgainButton,True);
  182.  
  183.         Window->DontFindButton = NewSimpleButton(Window->ScreenID,"Don't Find",
  184.             DONTFINDX,DONTFINDY,DONTFINDWIDTH,DONTFINDHEIGHT);
  185.         if (Window->DontFindButton == NIL)
  186.             {
  187.              FailurePoint7:
  188.                 DisposeSimpleButton(Window->FindAgainButton);
  189.                 goto FailurePoint6;
  190.             }
  191.  
  192.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",
  193.             CANCELX,CANCELY,CANCELWIDTH,CANCELHEIGHT);
  194.         if (Window->CancelButton == NIL)
  195.             {
  196.              FailurePoint8:
  197.                 DisposeSimpleButton(Window->DontFindButton);
  198.                 goto FailurePoint7;
  199.             }
  200.  
  201.         /* now do our loco event loop */
  202.         while (1)
  203.             {
  204.                 OrdType                            X;
  205.                 OrdType                            Y;
  206.                 ModifierFlags                Modifiers;
  207.                 MenuItemType*                MenuItem;
  208.                 char                                KeyPress;
  209.  
  210.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  211.                     {
  212.                         default:
  213.                             break;
  214.                         case eCheckCursor:
  215.                             if (TextEditIBeamTest(Window->StringToFind,X,Y)
  216.                                 || (TextEditIBeamTest(Window->StringToReplace,X,Y)))
  217.                                 {
  218.                                     SetIBeamCursor();
  219.                                 }
  220.                              else
  221.                                 {
  222.                                     SetArrowCursor();
  223.                                 }
  224.                             goto NoEventPoint;
  225.                             break;
  226.                         case eNoEvent:
  227.                          NoEventPoint:
  228.                             TextEditUpdateCursor(Window->ActiveTextEdit);
  229.                             break;
  230.                         case eMenuStarting:
  231.                             EnableMenuItem(MPaste);
  232.                             if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  233.                                 {
  234.                                     EnableMenuItem(MCut);
  235.                                     EnableMenuItem(MCopy);
  236.                                     EnableMenuItem(MClear);
  237.                                 }
  238.                             EnableMenuItem(MSelectAll);
  239.                             if (TextEditCanWeUndo(Window->ActiveTextEdit))
  240.                                 {
  241.                                     EnableMenuItem(MUndo);
  242.                                 }
  243.                             break;
  244.                         case eMenuCommand:
  245.                             if (MenuItem == MPaste)
  246.                                 {
  247.                                     TextEditDoMenuPaste(Window->ActiveTextEdit);
  248.                                 }
  249.                             else if (MenuItem == MCut)
  250.                                 {
  251.                                     TextEditDoMenuCut(Window->ActiveTextEdit);
  252.                                 }
  253.                             else if (MenuItem == MCopy)
  254.                                 {
  255.                                     TextEditDoMenuCopy(Window->ActiveTextEdit);
  256.                                 }
  257.                             else if (MenuItem == MClear)
  258.                                 {
  259.                                     TextEditDoMenuClear(Window->ActiveTextEdit);
  260.                                 }
  261.                             else if (MenuItem == MUndo)
  262.                                 {
  263.                                     TextEditDoMenuUndo(Window->ActiveTextEdit);
  264.                                     TextEditShowSelection(Window->ActiveTextEdit);
  265.                                 }
  266.                             else if (MenuItem == MSelectAll)
  267.                                 {
  268.                                     TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  269.                                 }
  270.                             else
  271.                                 {
  272.                                     EXECUTE(PRERR(AllowResume,
  273.                                         "DoFindDialog: Undefined menu option chosen"));
  274.                                 }
  275.                             break;
  276.                         case eKeyPressed:
  277.                             if (KeyPress == 13)
  278.                                 {
  279.                                     if ((Modifiers & eCommandKey) != 0)
  280.                                         {
  281.                                             /* command-return means actually interpret the return key */
  282.                                             /* in this implementation, multi-line search keys aren't */
  283.                                             /* supported, but in case we get around to changing it, */
  284.                                             /* we have this bit of code here */
  285.                                             /* TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress, */
  286.                                             /* Modifiers & ~eCommandKey); */
  287.                                         }
  288.                                      else
  289.                                         {
  290.                                             /* otherwise, leave the box & do the thing. */
  291.                                             FlashButton(Window->FindAgainButton);
  292.                                             ReturnCode = eFindAgain;
  293.                                             goto AllDonePoint;
  294.                                         }
  295.                                 }
  296.                             else if (KeyPress == 9)
  297.                                 {
  298.                                     if ((Modifiers & eCommandKey) != 0)
  299.                                         {
  300.                                             /* command-tab means actually interpret the return key */
  301.                                             TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress,
  302.                                                 (ModifierFlags)(Modifiers & ~eCommandKey));
  303.                                         }
  304.                                      else
  305.                                         {
  306.                                             /* otherwise, switch to the other text entry box */
  307.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  308.                                             if (Window->ActiveTextEdit == Window->StringToFind)
  309.                                                 {
  310.                                                     Window->ActiveTextEdit = Window->StringToReplace;
  311.                                                 }
  312.                                              else
  313.                                                 {
  314.                                                     Window->ActiveTextEdit = Window->StringToFind;
  315.                                                 }
  316.                                             TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  317.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  318.                                         }
  319.                                 }
  320.                             else if (KeyPress == eCancelKey)
  321.                                 {
  322.                                     FlashButton(Window->CancelButton);
  323.                                     ReturnCode = eFindCancel;
  324.                                     goto AllDonePoint;
  325.                                 }
  326.                             else
  327.                                 {
  328.                                     TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress,Modifiers);
  329.                                 }
  330.                             break;
  331.                         case eMouseDown:
  332.                             if (SimpleButtonHitTest(Window->FindFromStartButton,X,Y))
  333.                                 {
  334.                                     if (SimpleButtonMouseDown(Window->FindFromStartButton,X,Y,NIL,NIL))
  335.                                         {
  336.                                             ReturnCode = eFindFromStart;
  337.                                             goto AllDonePoint;
  338.                                         }
  339.                                 }
  340.                             else if (SimpleButtonHitTest(Window->FindAgainButton,X,Y))
  341.                                 {
  342.                                     if (SimpleButtonMouseDown(Window->FindAgainButton,X,Y,NIL,NIL))
  343.                                         {
  344.                                             ReturnCode = eFindAgain;
  345.                                             goto AllDonePoint;
  346.                                         }
  347.                                 }
  348.                             else if (SimpleButtonHitTest(Window->DontFindButton,X,Y))
  349.                                 {
  350.                                     if (SimpleButtonMouseDown(Window->DontFindButton,X,Y,NIL,NIL))
  351.                                         {
  352.                                             ReturnCode = eDontFind;
  353.                                             goto AllDonePoint;
  354.                                         }
  355.                                 }
  356.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  357.                                 {
  358.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  359.                                         {
  360.                                             ReturnCode = eFindCancel;
  361.                                             goto AllDonePoint;
  362.                                         }
  363.                                 }
  364.                             else if (TextEditHitTest(Window->StringToFind,X,Y))
  365.                                 {
  366.                                     if (Window->ActiveTextEdit != Window->StringToFind)
  367.                                         {
  368.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  369.                                             Window->ActiveTextEdit = Window->StringToFind;
  370.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  371.                                         }
  372.                                     TextEditDoMouseDown(Window->ActiveTextEdit,X,Y,Modifiers);
  373.                                 }
  374.                             else if (TextEditHitTest(Window->StringToReplace,X,Y))
  375.                                 {
  376.                                     if (Window->ActiveTextEdit == Window->StringToReplace)
  377.                                         {
  378.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  379.                                             Window->ActiveTextEdit = Window->StringToReplace;
  380.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  381.                                         }
  382.                                     TextEditDoMouseDown(Window->ActiveTextEdit,X,Y,Modifiers);
  383.                                 }
  384.                             break;
  385.                     }
  386.             }
  387.      AllDonePoint:
  388.         if (ReturnCode != eFindCancel)
  389.             {
  390.                 char*                NewFindStuff;
  391.                 char*                NewReplaceStuff;
  392.  
  393.                 NewFindStuff = TextEditGetRawData(Window->StringToFind,
  394.                     ""/*shouldn't be a linefeed*/);
  395.                 if (NewFindStuff == NIL)
  396.                     {
  397.                      EndFailure1:
  398.                         ReturnCode = eFindCancel;
  399.                         goto ReleaseAndExit;
  400.                     }
  401.                 NewReplaceStuff = TextEditGetRawData(Window->StringToReplace,
  402.                     ""/*shouldn't be a linefeed*/);
  403.                 if (NewReplaceStuff == NIL)
  404.                     {
  405.                      EndFailure2:
  406.                         ReleasePtr(NewFindStuff);
  407.                         goto EndFailure1;
  408.                     }
  409.                 ReleasePtr(*SearchKey);
  410.                 *SearchKey = NewFindStuff;
  411.                 ReleasePtr(*Replace);
  412.                 *Replace = NewReplaceStuff;
  413.             }
  414.      ReleaseAndExit:
  415.         DisposeTextEdit(Window->StringToFind);
  416.         DisposeTextEdit(Window->StringToReplace);
  417.         DisposeSimpleButton(Window->FindFromStartButton);
  418.         DisposeSimpleButton(Window->FindAgainButton);
  419.         DisposeSimpleButton(Window->DontFindButton);
  420.         DisposeSimpleButton(Window->CancelButton);
  421.         KillWindow(Window->ScreenID);
  422.         ReleasePtr((char*)Window);
  423.         return ReturnCode;
  424.     }
  425.